﻿
// LanTestMfcDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LanTestMfc.h"
#include "LanTestMfcDlg.h"
#include "afxdialogex.h"
#include <locale.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CLanTestMfcDlg dialog




CLanTestMfcDlg::CLanTestMfcDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CLanTestMfcDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLanTestMfcDlg::ThaiTest1()
{
	wchar_t sBuf[25]={0};
	wcscpy(sBuf, L"ดหกๆสแผหไๆห12121");//ดหกๆสแผหไๆห12121//L"中文"
	char dBuf[MAX_STR_LEN];
	if (MyMethod1(sBuf, dBuf))
	{
		int nLen = strlen(dBuf);
		GetDlgItem(IDC_STATIC_LAN)->SetWindowText(dBuf);
		GetDlgItem(IDC_EDIT1_LAN)->SetWindowText(dBuf);
	}
}

void CLanTestMfcDlg::ThaiTest2()
{
	std::fstream file1;
	char sBuf[MAX_STR_LEN]={0};

	file1.open("C:\\input\\input.txt");
	if (!file1.is_open())
		return;
	do 
	{
		file1.getline(sBuf, 40);
		string dBuf = UTF8ToANSI((sBuf));
		GetDlgItem(IDC_EDIT1_LAN)->SetWindowText(dBuf.c_str());
		int i = 0;
	} while (!file1.eof());
	file1.close();
}

std::string CLanTestMfcDlg::UTF8ToANSI(const char *str)
{
	string res;
	wchar_t *src;//宽字符类型 
	LPSTR lpres;//LPSTR被定义成是一个指向以NULL(‘\0’)结尾的32位ANSI字符数组指针 
	//执行转换的多字节字符所使用的字符集为UTF8，0表示不使用位标记，待转换的字符串为str，\
	设置-1会自动判断待转换字符串的长度 ，被转换字符串的缓冲区，0表示函数不执行转换
	//函数执行成功返回待转换字符串的缓冲区所需求的宽字符数大小 
	int tmp = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
	src=new wchar_t[tmp+5];
	//函数执行成功返回向缓冲区中写入的宽字符数
	MultiByteToWideChar(CP_UTF8, 0, str, -1, src, tmp);
	//ANSI编码，。。。，后两个参数可以忽略掉，就用NULL就好 
	tmp = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
	lpres=new char[tmp+5];
	WideCharToMultiByte(CP_ACP, 0, src, -1, lpres, tmp, NULL, NULL);
	res=lpres;
	delete []src;
	delete []lpres;
	return res;
}

int CLanTestMfcDlg::MyMethod1(wchar_t* sBuf, char* dBuf)
{
	DWORD dBufSize=WideCharToMultiByte(CP_OEMCP, 0, sBuf, -1, NULL,0,NULL, FALSE);
	//分配目标缓存
	memset(dBuf, 0, sizeof(dBuf));
	//转换
	int nLen = WideCharToMultiByte(CP_OEMCP, 0, sBuf, -1, dBuf, dBufSize, NULL, FALSE);
	return nLen;
}

int CLanTestMfcDlg::MyMethod2(wchar_t* sBuf, char* dBuf)
{

	return 0;
}

void CLanTestMfcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CLanTestMfcDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()


// CLanTestMfcDlg message handlers

BOOL CLanTestMfcDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	if (1)
	{
		ThaiTest1();
		ThaiTest2();
	}
	else
	{
		ThaiTest2();
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLanTestMfcDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CLanTestMfcDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CLanTestMfcDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

